package com.service.impl;

import com.entity.Comment;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.CommentMapper;
import com.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Milburn
 * @create 2021-03-02 10:35
 */
@Service
public class ICommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public Comment findCommentById(long comment_id) {
        return this.commentMapper.findCommentById(comment_id);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer addComment(Comment comment) {
        return this.commentMapper.addComment(comment);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer updateComment(Comment comment) {
        return this.commentMapper.updateComment(comment);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer deleteComment(long comment_id) {
        return this.commentMapper.deleteComment(comment_id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Comment> findAllComments() {
        return this.commentMapper.findAllComments();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Comment> findCommentsByMovieId(long movie_id) {
        return this.commentMapper.findCommentsByMovieId(movie_id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Comment> findAllCommentsBySplitPage(Integer page, Integer limit, String keyword) {
        PageHelper.startPage(page, limit);
        List<Comment> list = new ArrayList<>();
        if (keyword != null && !keyword.trim().equals("")) {
            System.out.println("keyword:" + keyword);
            list = this.commentMapper.findCommentsByUserName(keyword);
        } else {
            list = this.commentMapper.findAllComments();
        }

        return new PageInfo<>(list);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Comment> findCommentsByUserName(Integer page, Integer limit, String user_name) {

        PageHelper.startPage(page, limit);
        List<Comment> list = new ArrayList<>();
        list = this.commentMapper.findCommentsByUserName(user_name);
        PageInfo<Comment> info = new PageInfo<>(list);
        return info;
    }
}
